#ifndef ISTREAMLOADABLEWRITABLE_H
#define ISTREAMLOADABLEWRITABLE_H

#include <istream>
#include <ostream>
#include "Util.h"

class IStreamLoadableWritable
{
public:
	virtual bool TryLoad(std::istream& i) = 0;
	virtual bool TryDump(std::ostream& o) = 0;
	virtual bool IsDumpable() = 0;

public:
	static inline bool Load(std::istream& i, int& v)
	{
		char c[4];
		i.read(&c[0], 1);
		i.read(&c[1], 1);
		i.read(&c[2], 1);
		i.read(&c[3], 1);
		Util::Char42Int(v, c);
		return true;
	}
	static inline bool Load(std::istream& i, uint8_t& v)
	{
		i.read((char*)&v, 1);
		return true;
	}
	static inline bool Load(std::istream& i, int8_t& v)
	{
		i.read((char*)&v, 1);
		return true;
	}
public:
	static inline bool Dump(std::ostream& o, int v)
	{
		char c[4];
		Util::Int2Char4(v, c);
		o.write(&c[0], 1);
		o.write(&c[1], 1);
		o.write(&c[2], 1);
		o.write(&c[3], 1);
		return true;
	}
	static inline bool Dump(std::ostream& o, uint8_t v)
	{
		o.write((char*)&v, 1);
		return true;
	}
	static inline bool Dump(std::ostream& o, int8_t v)
	{
		o.write((char*)&v, 1);
		return true;
	}
};

#endif //ISTREAMLOADABLEWRITABLE_H
